package com.wtone.platform.gat.images.gat.service.impl;

import com.wtone.platform.gat.images.gat.core.Constants;
import com.wtone.platform.gat.images.gat.core.Response;
import com.wtone.platform.gat.images.gat.entity.Apedb;
import com.wtone.platform.gat.images.gat.mapper.ApedbMapper;
import com.wtone.platform.gat.images.gat.model.*;
import com.wtone.platform.gat.images.gat.service.ApesService;
import com.wtone.platform.gat.images.gat.utils.BenUtils;
import com.wtone.platform.gat.images.gat.utils.DateUtils;
import com.wtone.platform.gat.images.gat.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: aiLin
 * @Date: 2020/10/7 15:36
 * @desc: 采集设备相关接口
 */
@Slf4j
@Service
public class ApesServiceImpl implements ApesService {

    @Autowired
    HttpServletRequest request;

    @Autowired
    private ApedbMapper apedbMapper;

    @Override
    public ApeListObject queryApes(String ApeID, String Name, String Model, String IPAddr, String Port, String PlaceCode, String IsOnline, String UserId) {
        Apedb apedb = new Apedb();
        apedb.setApeid(ApeID!=null ? ApeID : "");
        apedb.setName(Name!=null ? Name : "");
        apedb.setModel(Model!=null ? Model : "");
        apedb.setIpaddr(IPAddr!=null ? IPAddr : "");
        apedb.setPort(Port !=null ? Port : "");
        apedb.setPlacecode(PlaceCode !=null ? PlaceCode : "");
        apedb.setIsonline(IsOnline !=null ? IsOnline : "");
        apedb.setUserid(UserId!=null ?UserId : "");
        List<Apedb> apedbs = apedbMapper.selectApedbList(apedb);
        ApeListObject apeListObject = new ApeListObject();
        ApeObject apeObject = new ApeObject();
        List<Ape> list = new ArrayList<Ape>();
        if(apedbs!=null && apedbs.size()<0){
            apeObject.setApes(list);
            apeListObject.setApeObject(apeObject);
            return apeListObject;
        }
        for(int i =0;i<apedbs.size();i++){
            Apedb ape = apedbs.get(i);
            try {
                Ape apeBen = (Ape) BenUtils.beanTobeanLowerCase2(ape, Ape.class);
                list.add(apeBen);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        apeObject.setApes(list);
        apeListObject.setApeObject(apeObject);
        return apeListObject;
    }

    /***
     * 采集设备口令修改
     * @param apeListObject
     * @return
     */
    @Override
    public ResponseStatusListModeObject updateApes(ApeListObject apeListObject) {
        String reqUrl = "/VIID/APEs";
        List<ResponseStatus> resp3 = new ArrayList<ResponseStatus>();
        //验证请求头 User-Identify
        String userLdentify = request.getHeader(Constants.REQTOKEN);

        ApeObject apeObject = apeListObject.getApeObject();
        List<Ape> apeList = apeObject.getApes();
        if(apeList==null || apeList.size()<=0){
            return Response.error(userLdentify,reqUrl, Constants.JSONREQ);
        }
        if(apeList!=null && apeList.size()>0){
            for(int i=0;i<apeList.size();i++) {
                Ape ape = apeList.get(i);
                ResponseStatus r = new ResponseStatus();
                //采集设备标识符
                try {
                    Apedb apedb = (Apedb)BenUtils.beanTobeanLowerCase(ape, Apedb.class);
                    apedb.setCreateBy("admin");
                    apedb.setCreateTime(DateUtils.getNowDate());

                    Apedb queryApedb = new Apedb();
                    queryApedb.setApeid(userLdentify);
                    //1.根据采集设备id 判断是否已经存在
                    List<Apedb> apedbs = apedbMapper.selectApedbList(queryApedb);
                    if(apedbs!=null && apedbs.size()>0){
                        log.info("采集设备：{}，已存在执行修改",apedb.getApeid());
                        if(StringUtils.isEmpty(apedb.getApeid())){
                            log.info("参数：{}，异常，略过修改",apedb.getApeid());
                        }
                        for(int j=0;j<apedbs.size();j++){
                            Apedb apedb1 = apedbs.get(j);
                            if(apedb1==null){
                                continue;
                            }
                            apedb.setId(apedb1.getId());
                            apedb.setUpdateBy("admin");
                            apedb.setUpdateTime(DateUtils.getNowDate());
                            apedb.setId("");
                            int mark = apedbMapper.updateApedb(apedb);
                            if(mark>=1){
                                r = Response.getResponOk(apedb.getApeid(),reqUrl,"修改采集设备成功");
                            }else{
                                r = Response.getResponOk(apedb.getApeid(),reqUrl,"修改采集设备失败");
                            }
                        }
                    }else{
                        log.info("新的采集设备消息：{}，执行加入",apedb.getApeid());
                        int mark = apedbMapper.insertApedb(apedb);
                        if(mark>=1){
                            r = Response.getResponOk(apedb.getApeid(),reqUrl,"加入采集设备成功");
                        }else{
                            r = Response.getResponOk(apedb.getApeid(),reqUrl,"加入采集设备失败");
                        }
                    }
                }catch (Exception e){
                    r = Response.getResponOk(ape.getApeID(),reqUrl,"采集设备失败"+e.toString());
                }
                resp3.add(r);
            }
        }
        return Response.ok(resp3);
    }

    /***
     * 添加采集设备
     * @param apeListObject
     * @return
     */
    @Override
    public ResponseStatusListModeObject AddApes(ApeListObject apeListObject) {
        String reqUrl = "/VIID/APEs";
        List<ResponseStatus> resp3 = new ArrayList<ResponseStatus>();
        //验证请求头 User-Identify
        String userLdentify = request.getHeader(Constants.REQTOKEN);
        ApeObject apeObject = apeListObject.getApeObject();
        List<Ape> apeList = apeObject.getApes();
        if(apeList==null || apeList.size()<=0){
            return Response.error(userLdentify,reqUrl, Constants.JSONREQ);
        }
        if(apeList!=null && apeList.size()>0){
            for(int i=0;i<apeList.size();i++) {
                Ape ape = apeList.get(i);
                ResponseStatus r = new ResponseStatus();
                //采集设备标识符
                try {
                    Apedb apedb = (Apedb)BenUtils.beanTobeanLowerCase(ape, Apedb.class);
                    apedb.setCreateBy("admin");
                    apedb.setCreateTime(DateUtils.getNowDate());
                    Apedb queryApedb = new Apedb();
                    queryApedb.setApeid(userLdentify);
                    //1.根据采集设备id 判断是否已经存在
                    List<Apedb> apedbs = apedbMapper.selectApedbList(queryApedb);
                    if(apedbs!=null && apedbs.size()>0){
                        r = Response.getResponOk(apedb.getApeid(),reqUrl,"采集设备："+apedb.getApeid()+"，已存在略过添加");
                    }else{
                        log.info("新的采集设备消息：{}，执行加入",apedb.getApeid());
                        int mark = apedbMapper.insertApedb(apedb);
                        if(mark>=1){
                            r = Response.getResponOk(apedb.getApeid(),reqUrl,"加入采集设备成功");
                        }else{
                            r = Response.getResponOk(apedb.getApeid(),reqUrl,"加入采集设备失败");
                        }
                    }
                }catch (Exception e){
                    if(String.valueOf(e).contains("ORA-00001")){
                        r = Response.getResponOk(ape.getApeID(),reqUrl,"采集设备失败，设备已存在");
                    }else{
                        r = Response.getResponOk(ape.getApeID(),reqUrl,"采集设备失败"+e.getMessage());
                    }
                }
                resp3.add(r);
            }
        }
        return Response.ok(resp3);
    }
}
